package com.ruoyi.drone.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.drone.domain.DroneOrganization;
import com.ruoyi.drone.mapper.DroneOrganizationMapper;
import com.ruoyi.drone.service.IDroneOrganizationService;
import com.ruoyi.drone.utils.PhotoUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * 组织方相关接口
 */
@RestController
@RequestMapping("/drone/organization")
public class DroneOrganizationController extends BaseController {

    @Autowired
    private DroneOrganizationMapper droneOrganizationMapper;

    @Autowired
    private IDroneOrganizationService droneOrganizationService;

    @Value("${ruoyi.profile:/home/ruoyi/uploadPath}")
    private String uploadPath;

    /**
     * 查询当前登录用户的组织信息
     */
    @GetMapping("/me")
    public AjaxResult getMyOrganization(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        if (userId == null) {
            return AjaxResult.error("未登录或用户信息缺失");
        }
        DroneOrganization organization = droneOrganizationMapper.selectDroneOrganizationByUserId(userId);
        if (organization == null) {
            return AjaxResult.success(null); // 前端据此判断为非组织或未注册
        }
        return AjaxResult.success(organization);
    }

    /**
     * 查询组织方信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DroneOrganization droneOrganization) {
        startPage();
        List<DroneOrganization> list = droneOrganizationService.selectDroneOrganizationList(droneOrganization);
        return getDataTable(list);
    }

    /**
     * 获取组织方信息详细信息
     */
    @GetMapping(value = "/{orgId}")
    public AjaxResult getInfo(@PathVariable("orgId") Long orgId) {
        return AjaxResult.success(droneOrganizationService.selectDroneOrganizationByOrgId(orgId));
    }

    /**
     * 新增组织方信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody DroneOrganization droneOrganization) {
        return toAjax(droneOrganizationService.insertDroneOrganization(droneOrganization));
    }

    /**
     * 修改组织方信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody DroneOrganization droneOrganization) {
        return toAjax(droneOrganizationService.updateDroneOrganization(droneOrganization));
    }

    /**
     * 删除组织方信息
     */
    @DeleteMapping("/{orgIds}")
    public AjaxResult remove(@PathVariable Long[] orgIds) {
        return toAjax(droneOrganizationService.deleteDroneOrganizationByOrgIds(orgIds));
    }

    /**
     * 审核组织方信息
     */
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody DroneOrganization droneOrganization) {
        return toAjax(droneOrganizationService.updateDroneOrganization(droneOrganization));
    }

    /**
     * 上传营业执照图片
     */
    @PostMapping("/upload/license")
    public AjaxResult uploadLicenseImage(@RequestParam("file") MultipartFile file) {
        try {
            String photoUrl = PhotoUploadUtils.uploadPhoto(file, "license", uploadPath);
            System.out.println("营业执照图片上传成功 - photoUrl: " + photoUrl);
            return AjaxResult.success(photoUrl);
        } catch (Exception e) {
            System.out.println("营业执照图片上传失败: " + e.getMessage());
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }
}


